# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=("util-linux" "libutil-linux" "libutil-linux-devel")
pkgver=2.26.2
pkgrel=1
pkgdesc="Miscellaneous system utilities for Linux"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.kernel.org/pub/linux/utils/util-linux/"
makedepends=('gettext-devel' 'libiconv-devel' 'ncurses-devel' 'libcrypt-devel' 'libtool' 'patch')
source=(https://www.kernel.org/pub/linux/utils/$pkgname/v${pkgver%.*}/$pkgname-$pkgver.tar.xz
        2.24.2-agetty.patch
        2.24.2-libblkid-topology.patch
        2.24.2-libintl.patch
        2.24.2-msysize.patch
        2.25.1-cygwin-include.patch
        2.25.1-ipc-utils.patch
        2.25.1-lslogins.patch
        2.25.1-relocate-sbin-to-usr-bin.patch)
sha256sums=('0e29bda142528a48a0a953c39ff63093651a4809042e1790fbd6aa8663fd9666'
            '0d6c812fab3deb34842dfb4313ce626e389c0961fe863a653ee1799a16614200'
            '347ff26c7750ef586dd50c3ac0be0b86604f8c4a4f148a0fc4e49d9ec55e2198'
            'a678a08fa18fc2ad34bc2a39bb0ff983f76803d4df742daf9d5e7b0d1d3b76f6'
            'e194d292a70ad8a10db6c365392a0cadba4cc31cdff4f8e60d6d4d82518a53df'
            '82545b7762445c5fbd43481f12bc8416f26783a70b3ce6a6ff3f42b2b9a1afc6'
            'd32fa6a4792de3b9414de607f83f3cd7571dd5bb741ba6086821cc634df9a4e1'
            'bac76eba04a41f1d7f3c403801e5b50d6aefbae4886d3d4e5bb1a3d4f0e17e63'
            'a1d64a62cbf9f6aaefbb2c40e76f56ec2240aa071d5c0c29910b05eec8a949d6')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p2 -i $srcdir/2.24.2-agetty.patch
  patch -p2 -i $srcdir/2.24.2-libblkid-topology.patch
  patch -p2 -i $srcdir/2.24.2-libintl.patch
  patch -p2 -i $srcdir/2.25.1-cygwin-include.patch
  patch -p2 -i $srcdir/2.25.1-ipc-utils.patch
  patch -p2 -i $srcdir/2.25.1-lslogins.patch
  patch -p1 -i $srcdir/2.24.2-msysize.patch
  patch -p1 -i $srcdir/2.25.1-relocate-sbin-to-usr-bin.patch
  
  autoreconf -fi
}

build() {
  [[ -d $srcdir/build-${CHOST} ]] && rm -rf $srcdir/build-${CHOST}
  mkdir -p $srcdir/build-${CHOST} && cd $srcdir/build-${CHOST}

  CPPFLAGS+=" -D__USE_LINUX_IOCTL_DEFS"
  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib \
    --libdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-libuuid \
    --enable-libblkid \
    --disable-libmount \
    --disable-mount \
    --disable-losetup \
    --disable-fsck \
    --disable-partx \
    --enable-uuidd \
    --disable-mountpoint \
    --disable-fallocate \
    --disable-unshare \
    --disable-nsenter \
    --disable-setpriv \
    --disable-eject \
    --enable-agetty \
    --enable-cramfs \
    --enable-bfs \
    --disable-fdformat \
    --disable-hwclock \
    --disable-wdctl \
    --disable-switch_root \
    --disable-pivot_root \
    --disable-ipcrm \
    --disable-ipcs \
    --disable-tunelp \
    --disable-kill \
    --disable-last \
    --disable-lslogins \
    --disable-utmpdump \
    --disable-line \
    --disable-mesg \
    --disable-raw \
    --enable-rename \
    --disable-reset \
    --disable-vipw \
    --disable-newgrp \
    --disable-chfn-chsh \
    --disable-login \
    --disable-nologin \
    --disable-sulogin \
    --disable-su \
    --disable-runuser \
    --enable-ul \
    --enable-more \
    --enable-pg \
    --disable-setterm \
    --disable-schedutils \
    --disable-wall \
    --disable-write \
    --disable-use-tty-group \
    --disable-makeinstall-chown \
    --disable-makeinstall-setuid \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    ac_cv_func___fpending=yes \
    SOLIB_LDFLAGS=-no-undefined

  make
  make DESTDIR=${srcdir}/dest install
}

package_util-linux() {
  pkgdesc="Collection of basic system utilities"
  depends=("coreutils" "libutil-linux" "libiconv")
  groups=('base')
  provides=('getopt')
  conflicts=('getopt')
  replaces=('getopt')

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_libutil-linux() {
  pkgdesc="Block device ID and Universally Unique ID libraries"
  provides=("libblkid" "libuuid" "libsmartcols")
  depends=("gcc-libs" "libintl" "msys2-runtime")

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libutil-linux-devel() {
  pkgdesc="Block device ID and Universally Unique ID headers and libraries."
  provides=("libblkid-devel" "libuuid-devel" "libsmartcols-devel")
  depends=("libutil-linux")
  options=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
}
